<?php


namespace backend\modules\statistics\controllers;

use backend\controllers\AdminController;
use common\models\shop\ShopHouseOrder;
use common\models\shop\ShopOrder;
use common\models\shop\ShopRaisingOrder;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use Yii;
/**
 * Class SiteController
 * @package backend\modules\finance\controllers
 * 用户统计相关
 */
class OrderController extends AdminController
{




    public function actionIndex()
    {

        $starttime = '2017-05-01';



        //猪肉订单
        $orders = [];
        $query = (new Query())->select(['count(id) as num','FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as time'])
            ->from(ShopOrder::tableName())
            ->where('createtime>=:start and createtime<=:now',[':start'=>strtotime($starttime),':now'=>time()])
            ->groupBy(['FROM_UNIXTIME(createtime,\'%Y-%m-%d\')'])
            ->indexBy('time');

        foreach ($query->batch() as $order) {
            $tmporders = ArrayHelper::getColumn($order,'time');
            $dissdays = \common\components\Helper::dateDifference($starttime,date('Y-m-d'));
            for ($i=0; $i<=$dissdays; $i++){
                $benddata = date('Y-m-d',strtotime($starttime.' +'.$i.' day'));
                if(in_array($benddata,$tmporders)){
                    $orders[] = intval($order[$benddata]['num']);
                }else{
                    $orders[] = 0;
                }
            }
        }
        unset($query);



        //认养订单
        $rasing = [];
        $query = (new Query())->select(['count(id) as num','FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as time'])
            ->from(ShopRaisingOrder::tableName())
            ->where('createtime>=:start and createtime<=:now',[':start'=>strtotime($starttime),':now'=>time()])
            ->groupBy(['FROM_UNIXTIME(createtime,\'%Y-%m-%d\')'])
            ->indexBy('time');

        foreach ($query->batch() as $order) {
            $tmporders = ArrayHelper::getColumn($order,'time');
            $dissdays = \common\components\Helper::dateDifference($starttime,date('Y-m-d'));
            for ($i=0; $i<=$dissdays; $i++){
                $benddata = date('Y-m-d',strtotime($starttime.' +'.$i.' day'));
                if(in_array($benddata,$tmporders)){
                    $rasing[] = intval($order[$benddata]['num']);
                }else{
                    $rasing[] = 0;
                }
            }
        }
        unset($query);


        //农家乐订单
        $house = [];
        $query = (new Query())->select(['count(id) as num','FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as time'])
            ->from(ShopHouseOrder::tableName())
            ->where('createtime>=:start and createtime<=:now',[':start'=>strtotime($starttime),':now'=>time()])
            ->groupBy(['FROM_UNIXTIME(createtime,\'%Y-%m-%d\')'])
            ->indexBy('time');
        foreach ($query->batch() as $order) {
            $tmporders = ArrayHelper::getColumn($order,'time');
            $dissdays = \common\components\Helper::dateDifference($starttime,date('Y-m-d'));
            for ($i=0; $i<=$dissdays; $i++){
                $benddata = date('Y-m-d',strtotime($starttime.' +'.$i.' day'));
                if(in_array($benddata,$tmporders)){
                    $house[] = intval($order[$benddata]['num']);
                }else{
                    $house[] = 0;
                }
            }
        }


        return $this->render('index',[
            'orders' => \GuzzleHttp\json_encode($orders),
            'rasing' => \GuzzleHttp\json_encode($rasing),
            'house' => \GuzzleHttp\json_encode($house)
        ]);
    }




}




